package com.cnswhy.cloud.server.core.oem;

import com.cnswhy.cloud.common.core.exception.BizException;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.common.redis.service.RedisService;
import com.cnswhy.cloud.server.common.entity.enumeration.AgentLevelType;
import com.cnswhy.cloud.server.common.entity.enumeration.BranchLevelType;
import com.cnswhy.cloud.server.core.constant.RedisConstant;
import com.cnswhy.cloud.server.core.entity.Customer;
import com.cnswhy.cloud.server.core.entity.SysConfig;
import com.cnswhy.cloud.server.core.service.SysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SystemService {
    public final static String cps_channel_id = "cps_channel_id";
    public final static String cps_notify_url = "cps_notify_url";
    public final static String reword_transfer_limit = "reword_transfer_limit";
    public final static String register_reword = "register_reword";
    public final static String first_quick_pay_reword = "first_quick_pay_reword";
    public final static int customer_final_level = 5;
    @Autowired
    RedisService redisService;
    @Autowired
    SysConfigService sysConfigService;

    public String getCpsChannelId(){
        try {
            String value = (String) redisService.hget(RedisConstant.SYS_CONFIG_2,
                    cps_channel_id);
            return value;
        } catch (Exception e) {
            throw new BizException("未设定默认渠道：cps_channel_id");
        }
    }
    public String getCpsNotifyUrl(){
        try {
            String value = (String) redisService.hget(RedisConstant.SYS_CONFIG_2,
                    cps_notify_url);
            return value;
        } catch (Exception e) {
            throw new BizException("未设定默认渠道：cps_notify_url");
        }
    }
    public int getRewordTransferLimit(){
        try {
            /**
             * 4. 营销活动奖励金转出，必须满足直推下级有3名；
             */
            SysConfig sysConfig = sysConfigService.getOne(Wraps.<SysConfig>lbQ().eq(SysConfig::getConfigKey, reword_transfer_limit));
            return Long.valueOf(sysConfig.getConfigValue()).intValue();
        } catch (Exception e) {
            return  0;
        }
    }
    public Long getRegisterReword(){
        try {
            /**
             * 2. 本级直推一个直属下级，得奖励2元；
             */
            SysConfig sysConfig = sysConfigService.getOne(Wraps.<SysConfig>lbQ().eq(SysConfig::getConfigKey, register_reword));
            return Long.valueOf(sysConfig.getConfigValue());
        } catch (Exception e) {
            return  0L;
        }
    }
    public Long getQuickPayReword(){
        try {
            /**
             * 3. 新注册客户完成首次快捷收款，得奖励8元；
             */
            /*Long value = (Long) redisService.hget(RedisConstant.SYS_CONFIG_2,
                    first_quick_pay_reword);
            return value;*/
            SysConfig sysConfig = sysConfigService.getOne(Wraps.<SysConfig>lbQ().eq(SysConfig::getConfigKey, first_quick_pay_reword));
            return Long.valueOf(sysConfig.getConfigValue());
        } catch (Exception e) {
            return  0L;
        }
    }
    public int getDefaultLevel(){
        return customer_final_level;
    }
    public int getCustomerLevel(Customer customer){
        if(BranchLevelType.ONE.eq(customer.getBranchLevelType())){
            return 1;
        }else if(BranchLevelType.TWO.eq(customer.getBranchLevelType())){
            return 2;
        }else if(AgentLevelType.ONE.eq(customer.getAgentLevelType())){
            return 3;
        }else if(AgentLevelType.TWO.eq(customer.getAgentLevelType())){
            return 4;
        }
        return customer_final_level;
    }
}
